package com.yuanzong.wuliu.service.app.auth;

import cn.hutool.core.collection.CollUtil;
import com.yuanzong.wuliu.common.vo.ApiResult;
import com.yuanzong.wuliu.controller.intercdeptor.AppHandlerInterceptorAdapter;
import com.yuanzong.wuliu.model.business.customer.BusinessCustomer;
import com.yuanzong.wuliu.model.business.customer.BusinessCustomerDaijiaxian;
import com.yuanzong.wuliu.model.business.customer.query.BusinessCustomerDaijiaxianListQuery;
import com.yuanzong.wuliu.service.business.customer.factory.BusinessCustomerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppRuntimeUserService {

    public ApiResult info() throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        BusinessCustomer runtimeUser = AppHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        BusinessCustomerDaijiaxianListQuery businessCustomerDaijiaxianListQuery = new BusinessCustomerDaijiaxianListQuery();
        businessCustomerDaijiaxianListQuery.setCustomerId(runtimeUser.getId());
        List<BusinessCustomerDaijiaxian> businessCustomerDaijiaxians = BusinessCustomerFactory.getBusinessCustomerDaijiaxianService().querySubject(businessCustomerDaijiaxianListQuery);
        // ===================================================================================//
        // ===== 返回结果
        // ===================================================================================//
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("runtimeUser", runtimeUser);
        resultMap.put("businessCustomerDaijiaxian", CollUtil.isNotEmpty(businessCustomerDaijiaxians) ? businessCustomerDaijiaxians.get(0) : null);
        return ApiResult.success(resultMap);
    }

    public ApiResult logout() throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        BusinessCustomer runtimeUser = AppHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        AppHandlerInterceptorAdapter.removeRuntimeUser(AppHandlerInterceptorAdapter.getRuntimeToken());
        return ApiResult.success();
    }
}
